package cn.iocoder.yudao.module.dt.controller.admin.wxnews;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dt.controller.admin.wxnews.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.wxnews.WxNewsDO;
import cn.iocoder.yudao.module.dt.convert.wxnews.WxNewsConvert;
import cn.iocoder.yudao.module.dt.service.wxnews.WxNewsService;

@Api(tags = "管理后台 - 新闻")
@RestController
@RequestMapping("/dt/wx-news")
@Validated
public class WxNewsController {

    @Resource
    private WxNewsService wxNewsService;

    @PostMapping("/create")
    @ApiOperation("创建新闻")
    @PreAuthorize("@ss.hasPermission('dt:wx-news:create')")
    public CommonResult<Integer> createWxNews(@Valid @RequestBody WxNewsCreateReqVO createReqVO) {
        return success(wxNewsService.createWxNews(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新新闻")
    @PreAuthorize("@ss.hasPermission('dt:wx-news:update')")
    public CommonResult<Boolean> updateWxNews(@Valid @RequestBody WxNewsUpdateReqVO updateReqVO) {
        wxNewsService.updateWxNews(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除新闻")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:wx-news:delete')")
    public CommonResult<Boolean> deleteWxNews(@RequestParam("id") Integer id) {
        wxNewsService.deleteWxNews(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得新闻")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:wx-news:query')")
    public CommonResult<WxNewsRespVO> getWxNews(@RequestParam("id") Integer id) {
        WxNewsDO wxNews = wxNewsService.getWxNews(id);
        return success(WxNewsConvert.INSTANCE.convert(wxNews));
    }

    @GetMapping("/list")
    @ApiOperation("获得新闻列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('dt:wx-news:query')")
    public CommonResult<List<WxNewsRespVO>> getWxNewsList(@RequestParam("ids") Collection<Integer> ids) {
        List<WxNewsDO> list = wxNewsService.getWxNewsList(ids);
        return success(WxNewsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得新闻分页")
    @PreAuthorize("@ss.hasPermission('dt:wx-news:query')")
    public CommonResult<PageResult<WxNewsRespVO>> getWxNewsPage(@Valid WxNewsPageReqVO pageVO) {
        PageResult<WxNewsDO> pageResult = wxNewsService.getWxNewsPage(pageVO);
        return success(WxNewsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出新闻 Excel")
    @PreAuthorize("@ss.hasPermission('dt:wx-news:export')")
    @OperateLog(type = EXPORT)
    public void exportWxNewsExcel(@Valid WxNewsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WxNewsDO> list = wxNewsService.getWxNewsList(exportReqVO);
        // 导出 Excel
        List<WxNewsExcelVO> datas = WxNewsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "新闻.xls", "数据", WxNewsExcelVO.class, datas);
    }

}
